package midone.exchangeskill.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.inject.Singleton;

@Singleton
public class SharedPreferencesUtil {

    public SharedPreferencesUtil(Context context) {
        this.context = context;
    }

    public Context context;
    public final static String DATASTORE = "data";

    public void put(String key, String value) {
        SharedPreferences settings = context.getSharedPreferences(DATASTORE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void putint(String key, int value) {
        SharedPreferences settings = context.getSharedPreferences(DATASTORE, Context.MODE_APPEND);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public void putiBoolean(String key, boolean value) {
        SharedPreferences settings = context.getSharedPreferences(DATASTORE, Context.MODE_APPEND);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public boolean getBoolean(String key) {
        SharedPreferences settings = context.getSharedPreferences(DATASTORE, Context.MODE_PRIVATE);
        boolean arg = settings.getBoolean(key, false);
        return arg;
    }

    public String getString(String key) {
        SharedPreferences settings = context.getSharedPreferences(DATASTORE, Context.MODE_PRIVATE);
        String arg = settings.getString(key, null);
        return arg;
    }

    public Integer getint(String key) {
        SharedPreferences settings = context.getSharedPreferences(DATASTORE, Context.MODE_PRIVATE);
        int arg = settings.getInt(key, 0);
        return arg;

    }

    public void clear() {
        SharedPreferences settings = context.getSharedPreferences(DATASTORE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();
        RxBus.getInstance().post(new RxBus.RxMessageEvent("LoginOut"));
    }

    public void remove(String key) {
        SharedPreferences settings = context.getSharedPreferences(DATASTORE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.remove(key);
        editor.commit();
    }

    public boolean setObjectToShare(Context context, Object object, String key) {
        SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(context);
        if (object == null) {
            SharedPreferences.Editor editor = share.edit().remove(key);
            return editor.commit();
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        try {
            oos = new ObjectOutputStream(byteArrayOutputStream);
            oos.writeObject(object);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        String objectStr = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
        try {
            byteArrayOutputStream.close();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putString(key, objectStr);
        return editor.commit();
    }

    public Object getObjectFromShare(Context context, String key) {
        SharedPreferences sharePre = PreferenceManager.getDefaultSharedPreferences(context);
        try {
            String wordBase64 = sharePre.getString(key, "");
            if (wordBase64 == null || wordBase64.equals("")) { // 不可少，否则在下面会报java.io.StreamCorruptedException
                return null;
            }
            byte[] objBytes = Base64.decode(wordBase64.getBytes(),
                    Base64.DEFAULT);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(objBytes);
            ObjectInputStream ois = new ObjectInputStream(byteArrayInputStream);
            Object obj = ois.readObject();
            byteArrayInputStream.close();
            ois.close();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}